<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.phone"
        placeholder="请输入手机号"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.name"
        placeholder="请输入商户名称"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-date-picker
        v-model="listQuery.startDay"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="选择开始日期时间"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-date-picker
        v-model="listQuery.endDay"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="选择结束日期时间"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate(2)"
      >
        添加主卡
      </el-button>
    </div>

    <el-table
      :key="key"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        prop="id"
        align="center"
        width="50"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="cardNum"
        label="主卡卡号"
        align="center"
        width="140"
      />
      <el-table-column
        label="类型"
        width=""
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.sourceId | typeFilter1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="子卡数量"
        width="100"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag size="medium" class="master-card"
                  @click="mainCard(scope.row)"
          > {{ scope.row.subNum }}
          </el-tag>

        </template>
      </el-table-column>
      <el-table-column
        prop="unSubNum"
        label="未分配子卡"
        align="center"
        width="100"
      />
      <el-table-column
        prop="bindSubSum"
        label="用户分配"
        align="center"
        width="80"
      />
      <el-table-column
        prop="bindOilNum"
        label="加油站分配"
        align="center"
        width="90"
      />
      <el-table-column
        label="卡面总额(元)"
        prop="totalMoney"
        min-width="90"
        align="center"
      />
      <el-table-column
        prop="currentMoney"
        label="未分配金额(元)"
        align="center"
        width="110"
      />
      <!--<el-table-column-->
        <!--label="累积分配(元)"-->
        <!--prop="money"-->
        <!--min-width="90"-->
        <!--align="center"-->
      <!--/>-->

      <!--<el-table-column-->
        <!--prop="payMoney"-->
        <!--label="实付金额"-->
        <!--align="center"-->
        <!--width=""-->
      <!--/>-->

      <el-table-column
        label="是否高挂"
        width=""
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.isHang | typeFilter }}
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        width=""
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      top="2vh"
      width="680px"
    >
      <el-form
        ref="temp"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="110px"
        style="width:550px; margin-left:50px;"
      >
        <el-form-item
          label="主卡卡号"
          prop="cardNum"
        >
          <el-input v-model="temp.cardNum" />
        </el-form-item>

        <el-form-item
          label="主卡类型"
          prop="sourceId"
        >
          <el-select
            v-model="temp.sourceId"
            class="filter-item"
            style="width:440px"
          >
            <el-option
              v-for="item in typeOptions"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item
          label="是否高挂卡"
          prop="isHang"
        >
          <el-select
            v-model="temp.isHang"
            class="filter-item"
            style="width:440px"
          >
            <el-option
              v-for="item in stateOptions"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button
          type="primary"
          @click="createData()"
        >
          {{ $t('table.confirm') }}
        </el-button>
      </div>

    </el-dialog>

  </div>
</template>
<script>
import permission from "@/directive/permission/index.js"; // 权限判断指令
import checkPermission from "@/utils/permission"; // 权限判断函数

import {
  getOilSupplyList,
  createOilSupply,
  getallAgent,
  getMainCardList,
  addOrUpdateOilCard
} from "@/api/v1";
import { supplyUnBind } from "@/api/user";

import waves from "@/directive/waves"; // Waves directive
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import { parseTime } from "@/utils";
import addImgall from "@/components/img";
import provinceData from "@/components/province";

export default {
  directives: { waves, permission },
  components: { Pagination, addImgall, provinceData },
  filters: {
    typeFilter(role) {
      //是否高挂 1是   2否
      switch (role) {
        case 1:
          return "是";
        case 2:
          return "否";
      }
    },
    typeFilter1(role) {
      //是否高挂 1加油   2加气
      switch (role) {
        case 1:
          return "中石油";
        case 2:
          return "中石化";
      }
    }
  },
  data() {
    return {
      editPhone: true,
      imageUrl: "",
      tableData: [],
      allAgent: [],
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      cooperation: [],
      listQuery: {
        page: 1,
        limit: 20,
        sort: "+id",
        phone: "",
        startDay: "",
        endDay: ""
      },
      stateOptions: [{ id: 1, value: "是" }, { id:2, value: "否" }],
      typeOptions: [{ id: 1, value: "中石油" }, { id: 2, value: "中石化" }],
      key: 1, // table key
      dialogFormVisible: false,
      rules: {
        cardNum: [{ required: true, message: "请输入卡号", trigger: "blur" }],
        isHang: [{ required: true, message: "请选择类型", trigger: "change" }],
        sourceId: [
          { required: true, message: "请选择类型", trigger: "change" }
        ],
        phone: [{ required: true, message: "请输入电话", trigger: "blur" }],
        legalPerson: [
          { required: true, message: "请输入姓名", trigger: "blur" }
        ],
        company: [
          { required: true, message: "请输入公司名称", trigger: "blur" }
        ]
      },
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "新增"
      },
      temp: {
        cardNum: "",
        sourceId: "",
        isHang: "",
        type: ""
      },
      isAdmin: true
    };
  },
  watch: {
    checkboxVal(valArr) {
      this.formThead = this.formTheadOptions.filter(
        i => valArr.indexOf(i) >= 0
      );
      this.key = this.key + 1; // 为了保证table 每次都会重渲 In order to ensure the table will be re-rendered each time
    }
  },

  created() {
    const id = this.$route.query.id;
    this.listQuery.supplyId = id;
    this.listQuery.type = 1; //主卡1    子卡2

    console.log(id);
    this.getList();
    // if (checkPermission(["admin"])) {
    //   this.getList2();
    // } else {
    //   this.isAdmin = false;
    // }
  },
  methods: {
    mainCard(row) {
      console.log(row);
      this.$router.push({
        path: "/daughterCard",
        query: {
          id: row.id,
          shop:this.$route.query.id
        }
      });
    },
    getProvince(data) {
      console.log("0000000000000000000000");
      console.log(data);
      this.temp.provinceCode = data.provinceCode;
      this.temp.cityCode = data.cityCode;
      this.temp.areaCode = data.areaCode;
      this.temp.address = data.address;
    },
    detail(row) {
      console.log(row);
      this.$router.push({
        path: "/bankCard",
        query: {
          relationId: row.id,
          relationType: 1,
          currentMoney: row.currentMoney,
          phone: row.phone,
          name: row.name,
          type: 2 // 1是省代  2是商户
        }
      });
    },
    getImg(data) {
      console.log(data);
      this.temp.businessLicense = data;
      this.imageUrl = data;
    },
    resetTemp() {
      this.temp = {
        cardNum: "",
        sourceId: "",
        isHang: "",
        type: ""
      };
    },
    handleCreate() {
      this.resetTemp();
      console.log(this.temp);
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.editPhone = false;
      this.$nextTick(() => {
        this.$refs["temp"].clearValidate();
      });
    },

    createData() {
      this.$refs["temp"].validate(valid => {
        this.temp.type = 1;
        this.temp.supplyId = this.$route.query.id;;

        if (valid) {
          addOrUpdateOilCard(this.temp).then(res => {
            // this.list.unshift(this.temp);
            this.dialogFormVisible = false;
            this.$notify({
              title: "成功",
              message: res.msg,
              type: "success",
              duration: 2000
            });
            this.getList();
          });
        }
      });
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row); // copy obj
      console.log(row);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.editPhone = true;
      this.$nextTick(() => {
        this.$refs["temp"].clearValidate();
      });
    },
    getList() {
      this.listLoading = true;
      getMainCardList(this.listQuery).then(res => {
        this.tableData = res.data.records;
        this.total = Number(res.data.total);
        console.log(this.tableData);
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },
    getList2() {
      this.listLoading = true;
      getallAgent(this.listQuery).then(res => {
        this.allAgent = res.data;
        console.log(this.allAgent);
      });
    },
    getCooperationAll() {
      cooperationAll().then(res => {
        this.cooperation = res.data;

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    }
  }
};
</script>
<style scoped>
  .master-card {
    cursor: pointer;
  }
</style>

